# -*- coding: UTF-8 -*-
from leetcode_helper import TestcaseHelper, SolutionBase, ListNode, array_to_list, list_to_array

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next


class Solution(SolutionBase):
    def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
        result_list = ListNode()
        head = result_list
        add_value = 0
        while l1 or l2:
            value1 = value2 = 0
            if l1:
                value1 = l1.val
            if l2:
                value2 = l2.val

            new_value = value1 + value2 + add_value
            if new_value >= 10:
                add_value = 1
                new_value = new_value - 10
            else:
                add_value = 0

            node = ListNode()
            node.val = new_value

            head.next = node
            head = node

            if l1 and l1.next:
                l1 = l1.next
            else:
                l1 = None

            if l2 and l2.next:
                l2 = l2.next
            else:
                l2 = None
        if add_value != 0:
            head.next = ListNode(val=add_value)

        return result_list.next

    def check_result(self, expect, result) -> bool:
        return self.check_result_array(expect, result)


if __name__ == '__main__':
    solution = Solution()
    testcases = [
        ([2, 4, 3], [5, 6, 4], [7, 0, 8]),
        ([0], [0], [0]),
        ([9, 9, 9, 9, 9, 9, 9], [9, 9, 9, 9], [8, 9, 9, 9, 0, 0, 0, 1])
    ]

    for case_item in testcases:
        # 获取测试用的case和期待的执行结果
        # TODO: 单独一条的Test case的组成随题目的不同而改变，需要按照题目来修改代码
        input1 = case_item[0]
        input2 = case_item[1]

        expect = case_item[len(case_item) - 1]
        # TODO: 调用对应方法，需要替换具体的方法
        exec_result = solution.addTwoNumbers(array_to_list(input1), array_to_list(input2))

        # 判断执行结果，输出提示信息
        check_result = solution.check_result(expect, list_to_array(exec_result))
        TestcaseHelper.print_case(check_result, case_item, list_to_array(exec_result))